#include "chooselevelscene.h"
#include "mypushbutton.h"
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QPainter>
#include <QDebug>
#include <QLabel>
#include <QSound>

ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    this->setFixedSize(390,590);

    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));

    this->setWindowTitle("选择场景关卡");

    //创建该界面的菜单栏
    QMenuBar *bar = menuBar();
    setMenuBar(bar);

    //创建开始菜单
    QMenu *startMenu = bar->addMenu("菜单");

    //创建退出菜单项
    QAction *quitaction = startMenu->addAction("退出");

    connect(quitaction,&QAction::triggered,[=]{
        this->close();
    });
    //选择按钮音效
    QSound *chooseSound = new QSound(":/res/TapButtonSound.wav",this);
    //返回按钮音效
    QSound *backSound = new QSound(":/res/BackButtonSound.wav",this);
    //返回按钮
    myPushButton *backBtn = new myPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    //点击返回
    connect(backBtn,&QPushButton::clicked,[=]{
        qDebug()<<"点击了返回按钮";
        backSound->play();
        //this->hide();
        emit this->chooseSceneBack();
    });

    //创建关卡按扭
    for(int i=0;i<20;i++)
    {
        myPushButton *levelBtn = new myPushButton(":/res/LevelIcon.png");
        levelBtn->setParent(this);
        levelBtn->move(22+(i%4)*80+levelBtn->width()*0.5, 150+(i/4)*70+levelBtn->height()*0.5);

        //监听按钮点击事件
        connect(levelBtn,&QPushButton::clicked,[=]{
            QString str = QString("你选择的关卡是第%1关").arg(i+1);
            qDebug()<<str;

            chooseSound->play();

            //进入到游戏场景
            this->hide();
            //创建游戏场景
            play = new PlayScene(i+1);
            play->show();
            play->setGeometry(this->geometry());

            connect(play,&PlayScene::PlayScencBack,[=]{

                //设置返回场景位置
                this->setGeometry(play->geometry());

                //销毁play对象
                this->show();
                delete play;
                play = NULL;

                //返回音效播放
                backSound->play();


                qDebug()<<"在游戏场景点击了返回按钮";
            });

        });



        QLabel *label = new QLabel;
        label->setParent(this);
        label->setFixedSize(levelBtn->width(),levelBtn->height());
        label->setText(QString::number(i+1));
        label->move(22+(i%4)*80+levelBtn->width()*0.5, 150+(i/4)*70+levelBtn->height()*0.5);

        //设置label对齐方式 垂直居中和水平居中
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);

        //设置鼠标穿透，按钮被label挡住
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }
}

void ChooseLevelScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;

    //背景
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //标题
    pix.load(":/res/Title.png");
    painter.drawPixmap((this->width()-pix.width())-0.5, 30, pix.width(), pix.height(), pix);


}
